package online.flowerinsnow.minecraft.bukkit.floweress.manager

import online.flowerinsnow.minecraft.bukkit.floweress.FlowerEss
import online.flowerinsnow.minecraft.bukkit.floweress.api.manager.IVisibleManager
import org.bukkit.Bukkit
import org.bukkit.entity.Player

class VisibleManagerImpl : IVisibleManager {
    private val invisibles = HashSet<Player>()
    override fun getPlayerVisible(player: Player): Boolean {
        return !invisibles.contains(player)
    }

    override fun setPlayerVisible(player: Player, visible: Boolean): Boolean {
        return if (!visible) hidePlayer(player)
        else showPlayer(player)
    }

    override fun hidePlayer(player: Player): Boolean {
        return if (invisibles.add(player)) {
            Bukkit.getOnlinePlayers().forEach {
                it.hidePlayer(FlowerEss.instance, player)
            }
            true
        } else false
    }

    override fun showPlayer(player: Player): Boolean {
        return if (invisibles.remove(player)) {
            Bukkit.getOnlinePlayers().forEach {
                it.showPlayer(FlowerEss.instance, player)
            }
            true
        } else false
    }

    override fun getInvisiblePlayers(): Set<Player> {
        return HashSet(invisibles)
    }
}